package com.dennis.nio;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.concurrent.EventExecutorGroup;

import java.net.InetSocketAddress;
import java.util.concurrent.atomic.LongAdder;

/**
 * @author Dennis Li
 * @date 2020/8/21 17:11
 */
public class CountHandler extends SimpleChannelInboundHandler<String> {

    private int count = 0;
    private int port;

    public int getCount() {
        return count;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        port = ((InetSocketAddress) ctx.channel().localAddress()).getPort();
        System.out.println(port + " Connected");
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        count++;
        ctx.writeAndFlush("This is from client-" + port);
    }
}
